package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    @PostMapping
    public R addSetMeal(@RequestBody SetmealDto dto){
        setmealService.saveWithDish(dto);
        return R.success("添加成功");
    }

    @GetMapping("page")
    public R page(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealDtoPage = new Page<>(page,pageSize);
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderByDesc("create_time");
        setmealService.page(setmealDtoPage,queryWrapper);

        Page<SetmealDto> setmealDtoPage1 = new Page<>();
        BeanUtils.copyProperties(setmealDtoPage,setmealDtoPage1,"records");
        List<SetmealDto> dtos = new ArrayList<>();
        List<Setmeal> records = setmealDtoPage.getRecords();
        for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Setmeal setmeal1 = setmealService.getById(setmeal.getId());
            setmealDto.setCategoryName(setmeal1.getName());
            dtos.add(setmealDto);
        }
        setmealDtoPage1.setRecords(dtos);
        return R.success(setmealDtoPage1);
    }

    @PostMapping("status/{status}")
    public R updateStatus(@PathVariable Integer status ,Long[] ids){
        LambdaUpdateWrapper<Setmeal> updateWrapper = Wrappers
                .lambdaUpdate(Setmeal.class)
                .set(Setmeal::getStatus, status)
                .in(Setmeal::getId, ids);
        setmealService.update(updateWrapper);
        return R.success("更改成功");
    }

    @DeleteMapping
    public R deleteByIds(Long[] ids){

        int count = setmealService.deleteByIds(ids);

        if (count == 0){
            return R.error("删除失败，你选中的商品全部都在启售中！！！");
        }
        return R.success("成功删除了"+count+"个套餐");
    }

    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(setmeal.getStatus() != null, "status", setmeal.getStatus());
        queryWrapper.eq(setmeal.getCategoryId() != null, "category_id", setmeal.getCategoryId());
        queryWrapper.orderByDesc("update_time");

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
